package com.data.modules.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.modules.ai.entity.AiRule;
import com.data.modules.ai.entity.AiRuleExcel;
import com.data.modules.ai.mapper.AiRuleMapper;
import com.data.modules.ai.service.IAiRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 自动化流程规则数据
 * @Author: data-boot
 * @Date:   2022-03-03
 * @Version: V1.0
 */
@Service
@Slf4j
public class AiRuleServiceImpl extends ServiceImpl<AiRuleMapper, AiRule> implements IAiRuleService {

    @Override
    public Boolean saveAiRuleByExcel(JSONArray jsonArrayData){
        Boolean bool = true;

        Map<String,String> columnsMap = new HashMap<>();
        Field[] field = AiRuleExcel.class.getDeclaredFields();
        for (int i=0;i<field.length;i++) {
            columnsMap.put(i+"",field[i].getName());
        }
        Long start = System.currentTimeMillis();
        Map<Integer, Map<String, Object>> saveMap = new HashMap<>();
        try {
            for (Object object : jsonArrayData) {
                JSONObject jsonObject = (JSONObject) object;
                Integer r = jsonObject.getInteger("r");
                Integer c = jsonObject.getInteger("c");
                if (r > 0) {
                    Map<String, Object> orderMap;
                    if (saveMap.containsKey(r)) {
                        orderMap = saveMap.get(r);
                    } else {
                        orderMap = new HashMap<>();
                    }
                    JSONObject jsonObjectValue = jsonObject.getJSONObject("v");
                    //判断是否有M，如果有则保存
                    if (jsonObjectValue.containsKey("m")) {
                        Object value = jsonObjectValue.get("v");
                        String column = columnsMap.get(c.toString());
                        //log.info("c:{},column:{},value:{}", c, column, value);
                        orderMap.put(column, value);
                        saveMap.put(r, orderMap);
                    }
                }
            }
            //log.info("接收数据：{}", JSON.toJSONString(saveMap));
            for (Object value : saveMap.values()) {
                Map<String, Object> valueMap = (Map<String, Object>) value;
                AiRule aiRule = BeanUtil.mapToBean(valueMap, AiRule.class, true);
                log.info("aiRule：{}", JSON.toJSONString(aiRule));

                AiRule workOrderOld = this.getById(aiRule.getId());
                aiRule.setStatus(1);
                this.save(aiRule);

            }
        }catch(NumberFormatException ne){
            log.error("更新AiRule数据异常：{}", ne.getMessage());
            ne.printStackTrace();

        }catch (Exception e)
        {
            log.error("更新AiRule数据异常：{}", e.getMessage());
            e.printStackTrace();
            bool = false;
        }
        Long end = System.currentTimeMillis();
        log.info("修改对象excel AiRule共耗时：{}毫秒", (start-end));
        return bool;
    }
}
